package com.ddone.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddone.common.exception.Assert;
import com.ddone.common.result.ResponseEnum;
import com.ddone.finance.hfb.FormHelper;
import com.ddone.finance.hfb.HfbConst;
import com.ddone.finance.hfb.RequestHelper;
import com.ddone.finance.mapper.UserBindMapper;
import com.ddone.finance.mapper.UserInfoMapper;
import com.ddone.finance.pojo.entity.UserBind;
import com.ddone.finance.pojo.entity.UserInfo;
import com.ddone.finance.pojo.enums.UserBindEnum;
import com.ddone.finance.pojo.vo.UserBindVo;
import com.ddone.finance.service.UserBindService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author ddone
 * @since 2024-04-10
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public String commitBindUser(UserBindVo userBindVo, Long userId) {
        //查询身份证号码是否绑定
        LambdaQueryWrapper<UserBind> wrapper = Wrappers.lambdaQuery(UserBind.class);
        wrapper.eq(UserBind::getIdCard, userBindVo.getIdCard())
                .ne(UserBind::getUserId, userId);
        UserBind userBind = baseMapper.selectOne(wrapper);
        Assert.isNull(userBind, ResponseEnum.USER_BIND_IDCARD_EXIST_ERROR);

        // 查询用户绑定信息
        userBind = baseMapper.selectOne(Wrappers.lambdaQuery(UserBind.class).eq(UserBind::getUserId, userId));
        //判断是否有绑定记录
        if (userBind == null) {
            userBind = new UserBind();
            BeanUtils.copyProperties(userBindVo, userBind);
            userBind.setUserId(userId);
            userBind.setStatus(UserBindEnum.NO_BIND.getStatus());
            baseMapper.insert(userBind);
        } else {
            //曾经跳转到托管平台，但是未操作完成，此时将用户最新填写的数据同步到userBind对象
            BeanUtils.copyProperties(userBindVo, userBind);
            baseMapper.updateById(userBind);
        }
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("agentId", HfbConst.AGENT_ID);
        paramMap.put("agentUserId", userId);
        paramMap.put("idCard", userBindVo.getIdCard());
        paramMap.put("personalName", userBindVo.getName());
        paramMap.put("bankType", userBindVo.getBankType());
        paramMap.put("bankNo", userBindVo.getBankNo());
        paramMap.put("mobile", userBindVo.getMobile());
        paramMap.put("returnUrl", HfbConst.USERBIND_RETURN_URL);
        paramMap.put("notifyUrl", HfbConst.USERBIND_NOTIFY_URL);
        paramMap.put("timestamp", RequestHelper.getTimestamp());
        paramMap.put("sign", RequestHelper.getSign(paramMap));
        //构建充值自动提交表单
        String formStr = FormHelper.buildForm(HfbConst.USERBIND_URL, paramMap);
        return formStr;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void notify(Map<String, Object> paramMap) {

        String bindCode = (String)paramMap.get("bindCode");
        //会员id
        String agentUserId = (String)paramMap.get("agentUserId");

        //根据user_id查询user_bind记录
        LambdaQueryWrapper<UserBind> wrapper = new LambdaQueryWrapper<UserBind>();
        wrapper.eq(UserBind::getUserId, agentUserId);

        //更新用户绑定表
        UserBind userBind = baseMapper.selectOne(wrapper);
        userBind.setBindCode(bindCode);
        userBind.setStatus(UserBindEnum.BIND_OK.getStatus());
        baseMapper.updateById(userBind);

        //更新用户表
        UserInfo userInfo = userInfoMapper.selectById(agentUserId);
        userInfo.setBindCode(bindCode);
        userInfo.setName(userBind.getName());
        userInfo.setIdCard(userBind.getIdCard());
        userInfo.setBindStatus(UserBindEnum.BIND_OK.getStatus());
        userInfoMapper.updateById(userInfo);
    }
}
